package com.hm.scala

import java.util.ArrayList

import scala.collection.mutable.ArrayBuffer

object ArrayTest {

  def main(args: Array[String]): Unit = {

    //调用java函数
    var array = new ArrayList[String]();
    {
      array.add("aa");
      array.add("bb");
      array.add("cc");
      array.add("d");
    }

    array.forEach(str => println(str))
    println("--------华丽分割线------------")

    //声明固定长度scala数组
    var numArray = new Array[Int](3);
    {
      numArray(0) = 12;
      numArray(1) = 14;
      numArray(2) = 15;
    }

    for (num <- 0 until numArray.length) {
      println(numArray(num))
    }

    println("--------华丽分割线------------")
    //声明长度无线数组
    var buffer = new ArrayBuffer[Int];
    {
      //在尾部添加元素
      buffer += 13
      //在尾部添加多个元素
      buffer += (1, 232, 43, 454, 2)
      //通过++ 往数组后面追加集合
      buffer ++= Array(100, 200, 300)

      for (num <- 0 until buffer.length) {
        println(buffer(num))
      }

      for (num <- buffer) {
        print(num + ",");
      }

      println();

      //对元素每一个值都添加相同的值

      var nm = for (num <- buffer) yield num + 2
      println("添加后的值是" + nm)

      var nm2 = for (num <- buffer if num % 2 == 0) yield num + 1;
      println("取模后的值加1是" + nm2)

      //filter 、map

      var nm3 = buffer.filter(_ % 2 == 0);
      println("filter取模后数据" + nm3)

      var nm4 = nm3.map(_ + 1) //对每一个元素加一
      println("map后数据是" + nm4)

      //合并
      var nm5 = buffer.filter(_ % 2 == 0).map(_ + 1)
      println("合并后结果是" + nm5)

      println("--------华丽分割线------------")

      println("最大值" + buffer.max)
      println("最小值" + buffer.min)
      println("除了第一个元素外其他元素" + buffer.tail)
      println("最后一个元素" + buffer.last)
      println("求和" + buffer.sum)

      println("--------华丽分割线------------")

      var mut = Array.ofDim[Int](3, 4)
      mut(1)(2) = 15;
      println("二位数组取值" + mut(1)(2))

      println("--------华丽分割线（拉链操作）------------")

      var ay1 = Array(1, 2, 3)
      var ay2 = Array(4, 5, 6)

      var ay = ay1.zip(ay2)
      
      println("拉链后值(可以看成合并成了一个二维数组)"+ay.toString())

      ay.foreach(na =>{
        println(na)
      })
      
    }

  }
}